define(function(require, exports, module) {
  'use strict';

  var $ = require('$');
  var Widget = require('widget');

  var io = require('../../common/io');

  var Similar = module.exports = Widget.extend({
    defaults: {
      template: require('./similar.handlebars'),
      question: '',
      insert: function () {
        this.field.after(this.element);
      }
    },
    setup: function() {
      var self = this;
      this.field = $(this.option('field'));
      if (!this.field.length) {
        throw Error('field is invalid!');
      }
      this.renderList();
    },
    setQuestion: function(question) {
      this.option('question', question);
      this.renderList();
    },
    renderList: function() {
      var self = this;
      var question = this.option('question');

      if (question === '') {
        return;
      }
      io.get('similarList', {docSubject: this.option('question'), pageSize: 5}, function() {
        self.data({list: this.data.listData});
        self.render();
      });
    }
  });
});
